﻿namespace AnyOffice.BIZ
{
    using AnyOffice.DAL;
    using AnyOffice.Model;
    using AnyOffice.WorkFlowRegex;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;

    public class WorkFlowBiz
    {
        public DataTable ByUser(SqlConnection conn, int runId)
        {
            string commandText = "DECLARE @UserTable TABLE(UserId nvarchar(20), UserName nvarchar(50), PrivNo int , DeptNo int, UserNo int) DECLARE @PrcsUser nvarchar(4000) DECLARE @PrcsDept nvarchar(2000) DECLARE @PrcsPriv nvarchar(2000) DECLARE Process_Cursor CURSOR FOR SELECT CAST([PrcsUser] AS nvarchar(4000)), CAST([PrcsDept] AS nvarchar(2000)), CAST([PrcsPriv] AS nvarchar(2000)) FROM [dbo].[Flow_Run], [dbo].[Flow_Process] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Process].[FlowId] = [Flow_Run].[FlowId] OPEN Process_Cursor FETCH NEXT FROM Process_Cursor INTO @PrcsUser,@PrcsDept,@PrcsPriv WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO @UserTable SELECT [Users].[UserId], [Users].[UserName], [User_Priv].[PrivNo], [Department].[DeptNo], [Users].[UserNo] FROM [dbo].[Users], [dbo].[Department], [dbo].[User_Priv] WHERE ((CHARINDEX(',' + [Users].[UserId] + ',',',' + ISNULL(@PrcsUser,'') + ',') > 0) OR (CHARINDEX(',' + CAST([Users].[DeptId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsDept,'') + ',') > 0) OR (CHARINDEX(',' + CAST([Users].[PrivId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsPriv,'') + ',') > 0)) AND [Users].[DeptId] = [Department].[DeptId] AND [Users].[PrivId] = [User_Priv].[PrivId] ORDER BY [User_Priv].[PrivNo] ASC, [Department].[DeptNo] ASC, [Users].[UserNo] ASC FETCH NEXT FROM Process_Cursor INTO @PrcsUser,@PrcsDept,@PrcsPriv END CLOSE Process_Cursor DEALLOCATE Process_Cursor SELECT DISTINCT * FROM @UserTable ORDER BY [PrivNo] ASC, [DeptNo] ASC, [UserNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Users";
            return set.Tables[0];
        }

        public DataTable ByUserId(SqlConnection conn, string userId)
        {
            string commandText = "SELECT DISTINCT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [UserId] = @UserId ";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable CreateUser(SqlConnection conn, int runId)
        {
            string commandText = "SELECT [Users].[UserId], [Users].[UserName] FROM [dbo].[Flow_Run_Prcs], [dbo].[Users] WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[PrcsId] = 1 AND [Flow_Run_Prcs].[UserId] = [Users].[UserId]";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Users";
            return set.Tables[0];
        }

        public void Delete(SqlConnection conn, int runId, string userId, bool isAdministrator)
        {
            string commandText = "DECLARE @IsCanDelete bit SET @IsCanDelete = 0 IF @IsAdministrator =1 SET @IsCanDelete = 1 IF @IsAdministrator = 0 BEGIN IF EXISTS ( SELECT [Flow_Run_Prcs].[RunId] FROM [dbo].[Flow_Run_Prcs], [dbo].[Flow_Run], [dbo].[Flow_Type] WHERE [Flow_Run_Prcs].[PrcsId] = 1 AND [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[RunId] = [Flow_Run].[RunId] AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND ( (([Flow_Run_Prcs].[UserId] = @UserId) AND ([Flow_Run_Prcs].[OpFlag] = 1) AND ([Flow_Run].[Status] = 0)) OR (CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)) ,'') + ',') > 0) ) ) SET @IsCanDelete = 1 END IF @IsCanDelete = 1 BEGIN UPDATE [dbo].[Attachment] SET [DeleteFlag] = 1 WHERE [AttachmentId] IN ( SELECT [AttachmentId] FROM [dbo].[Flow_Run_Feedback] WHERE [RunId] = @RunId AND ISNULL([AttachmentId],'') <> '' ) UPDATE [dbo].[Attachment] SET [DeleteFlag] = 1 WHERE [AttachmentId] IN ( SELECT [AttachmentId] FROM [dbo].[Flow_Run] WHERE [RunId] = @RunId AND ISNULL([AttachmentId],'') <> '' ) DELETE FROM [dbo].[Flow_Run_Data] WHERE [RunId] = @RunId DELETE FROM [dbo].[Flow_Run_Feedback] WHERE [RunId] = @RunId DELETE FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId DELETE FROM [dbo].[Flow_Run] WHERE [RunId] = @RunId END";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = userId;
            commandParameters[2] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[2].Value = isAdministrator;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public int DeleteForData(SqlConnection conn, int flowId, DateTime beginDate, DateTime endDate)
        {
            int num = 0;
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@FlowId", SqlDbType.Int);
            commandParameters[0].Value = flowId;
            commandParameters[1] = new SqlParameter("@BeginDate", SqlDbType.DateTime);
            commandParameters[1].Value = beginDate;
            commandParameters[2] = new SqlParameter("@EndDate", SqlDbType.DateTime);
            commandParameters[2].Value = endDate;
            string commandText = "SELECT COUNT([RunId]) AS rowNums FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND [AddTime] >= @BeginDate AND [AddTime] <= @EndDate";
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != DBNull.Value)
            {
                num = Convert.ToInt32(obj2);
            }
            string str2 = "UPDATE [dbo].[Attachment] SET [DeleteFlag] = 1 WHERE [AttachmentId] IN ( SELECT [Flow_Run_Feedback] .[AttachmentId] FROM [dbo].[Flow_Run_Feedback], [dbo].[Flow_Run] WHERE [Flow_Run_Feedback].[RunId] = [Flow_Run].[RunId] AND [Flow_Run].[FlowId] = @FlowId AND [Flow_Run].[AddTime] >= @BeginDate AND [Flow_Run].[AddTime] <= @EndDate AND ISNULL([Flow_Run_Feedback] .[AttachmentId],'') <> '' ) UPDATE [dbo].[Attachment] SET [DeleteFlag] = 1 WHERE [AttachmentId] IN ( SELECT [AttachmentId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND [AddTime] >= @BeginDate AND [AddTime] <= @EndDate AND ISNULL([AttachmentId],'') <> '' ) DELETE FROM [dbo].[Flow_Run_Data] WHERE [RunId] IN (SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND [AddTime] >= @BeginDate AND [AddTime] <= @EndDate) DELETE FROM [dbo].[Flow_Run_Feedback] WHERE [RunId] IN (SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND [AddTime] >= @BeginDate AND [AddTime] <= @EndDate) DELETE FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] IN (SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND [AddTime] >= @BeginDate AND [AddTime] <= @EndDate) DELETE FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND [AddTime] >= @BeginDate AND [AddTime] <= @EndDate";
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, str2, commandParameters);
            return num;
        }

        public void DeleteForDesign(SqlConnection conn, int flowId)
        {
            this.Empty(conn, flowId);
            string commandText = "DELETE FROM [dbo].[Flow_Process] WHERE [FlowId] = @FlowId DELETE FROM [dbo].[Flow_Type] WHERE [FlowId] = @FlowId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@FlowId", SqlDbType.Int) };
            commandParameters[0].Value = flowId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public void Empty(SqlConnection conn, int flowId)
        {
            string commandText = "UPDATE [dbo].[Attachment] SET [DeleteFlag] = 1 WHERE [AttachmentId] IN ( SELECT [Flow_Run_Feedback] .[AttachmentId] FROM [dbo].[Flow_Run_Feedback], [dbo].[Flow_Run] WHERE [Flow_Run_Feedback].[RunId] = [Flow_Run].[RunId] AND [Flow_Run].[FlowId] = @FlowId AND ISNULL([Flow_Run_Feedback] .[AttachmentId],'') <> '' ) UPDATE [dbo].[Attachment] SET [DeleteFlag] = 1 WHERE [AttachmentId] IN ( SELECT [AttachmentId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId AND ISNULL([AttachmentId],'') <> '' ) DELETE FROM [dbo].[Flow_Run_Data] WHERE [RunId] IN (SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId) DELETE FROM [dbo].[Flow_Run_Feedback] WHERE [RunId] IN (SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId) DELETE FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] IN (SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId) DELETE FROM [dbo].[Flow_Run] WHERE [FlowId] = @FlowId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@FlowId", SqlDbType.Int) };
            commandParameters[0].Value = flowId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public string HiddenItem(SqlConnection conn, int runId, string userId)
        {
            string commandText = "SELECT [Flow_Process].[HiddenItem] FROM [dbo].[Flow_Run], [dbo].[Flow_Process], [dbo].[Flow_Run_Prcs] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run_Prcs].[FlowPrcs] = [Flow_Process].[PrcsId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Process].[HiddenItem] IS NOT NULL";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = userId;
            string str2 = string.Empty;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            if (set.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    str2 = str2 + set.Tables[0].Rows[i]["HiddenItem"].ToString() + ",";
                }
            }
            return str2;
        }

        public bool IsOp(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "DECLARE @OpFlag int SELECT @OpFlag = OpFlag FROM [dbo].[Flow_Run_Prcs] WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[PrcsId] = @PrcsId AND [Flow_Run_Prcs].[UserId] = @UserId SELECT ISNULL(@OpFlag,0)";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            return ((obj2 != null) && Convert.ToBoolean(obj2));
        }

        public bool IsView(SqlConnection conn, int runId, string userId)
        {
            string commandText = "DECLARE @IsView bit IF EXISTS( SELECT [Flow_Run].[RunId] FROM [dbo].[Flow_Run], [dbo].[Flow_Type] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND ( CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)) ,'') + ',') > 0 OR CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[QueryUser] AS nvarchar(2000)) ,'') + ',') > 0 ) ) SET @IsView = 1 ELSE BEGIN IF EXISTS( SELECT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [UserId] = @UserId ) SET @IsView = 1 END SELECT ISNULL(@IsView ,0)";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = userId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            return ((obj2 != null) && Convert.ToBoolean(obj2));
        }

        public DataTable Manage(SqlConnection conn, string userId)
        {
            string commandText = "DECLARE @WorkTable TABLE( RunId int, RunName nvarchar(200) DEFAULT '', PrcsId int, UserId nvarchar(20), PrcsTime DateTime, PrcsFlag int, PrcsName nvarchar(200) DEFAULT '', TimeOut int, FlowId int, FlowName nvarchar(200) DEFAULT '', FlowType int, SortName nvarchar(200) DEFAULT '', UserName nvarchar(50), NowTime DateTime ) INSERT INTO @WorkTable SELECT DISTINCT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[UserId], [Flow_Run_Prcs].[PrcsTime], [Flow_Run_Prcs].[PrcsFlag], [Flow_Process].[PrcsName], [Flow_Process].[TimeOut], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Users].[UserName], GETDATE() FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Process], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Process].[PrcsId] = [Flow_Run_Prcs].[FlowPrcs] AND [Flow_Run_Prcs].[PrcsFlag] IN (1,2) AND [Flow_Run_Prcs].[OpFlag] = 1 AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 1 AND CHARINDEX(','+ @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)) ,'') + ',') > 0 AND [Flow_Run_Prcs].[UserId] = [Users].[UserId] INSERT INTO @WorkTable SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[UserId], [Flow_Run_Prcs].[PrcsTime], [Flow_Run_Prcs].[PrcsFlag], '', NULL, [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Users].[UserName], GETDATE() FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs].[PrcsFlag] IN (1,2) AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 2 AND CHARINDEX(','+ @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)) ,'') + ',') > 0 AND [Flow_Run_Prcs].[UserId] = [Users].[UserId] SELECT * FROM @WorkTable AS Flow_Run ORDER BY [RunId] DESC, [PrcsFlag] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable MyTable(SqlConnection conn, string userId)
        {
            string commandText = "SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[PrcsFlag], [Flow_Type].[FlowName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Type] WHERE [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs].[PrcsFlag] IN(1,2 ) AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] ORDER BY [Flow_Run].[RunId] DESC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable NeedProcess(SqlConnection conn, string userId)
        {
            string commandText = "DECLARE @WorkTable TABLE( RunId int, RunName nvarchar(200) DEFAULT '', PrcsId int, FlowPrcs int, PrcsTime DateTime, PrcsName nvarchar(200) DEFAULT '', TimeOut int, FlowId int, FlowName nvarchar(200) DEFAULT '', FlowType int, SortName nvarchar(200) DEFAULT '', OpFlag int, TopFlag int, CreateUser nvarchar(20) DEFAULT '', CreateUserName nvarchar(50) DEFAULT '', NowTime DateTime ) INSERT INTO @WorkTable SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[FlowPrcs], [Flow_Run_Prcs].[PrcsTime], [Flow_Process].[PrcsName], [Flow_Process].[TimeOut], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Flow_Run_Prcs].[OpFlag], [Flow_Run_Prcs].[TopFlag], [Flow_Run_Prcs2].[UserId] AS [CreateUser], [Users].[UserName] AS [CreateUserName], GETDATE() FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Run_Prcs] AS [Flow_Run_Prcs2], [dbo].[Flow_Process], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run_Prcs].[PrcsFlag] = 1 AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs2].[RunId] = [Flow_Run].[RunId] AND [Flow_Run_Prcs2].[PrcsId] = 1 AND [Flow_Run_Prcs2].[UserId] = [Users].[UserId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Process].[PrcsId] = [Flow_Run_Prcs].[FlowPrcs] AND [Flow_Type].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 1 ORDER BY [Flow_Run].[RunId] DESC INSERT INTO @WorkTable SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[FlowPrcs], [Flow_Run_Prcs].[PrcsTime], '', NULL, [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Flow_Run_Prcs].[OpFlag], [Flow_Run_Prcs].[TopFlag], [Flow_Run_Prcs2].[UserId] AS [CreateUser], [Users].[UserName] AS [CreateUserName], GETDATE() FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Run_Prcs] AS [Flow_Run_Prcs2], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run_Prcs].[PrcsFlag] = 1 AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs2].[RunId] = [Flow_Run].[RunId] AND [Flow_Run_Prcs2].[PrcsId] = 1 AND [Flow_Run_Prcs2].[UserId] = [Users].[UserId] AND [Flow_Type].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 2 ORDER BY [Flow_Run].[RunId] DESC SELECT * FROM @WorkTable AS Flow_Run ORDER BY RunId DESC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public int New(SqlConnection conn, int flowId, string runName, string userId)
        {
            int runId = -1;
            string commandText = "DECLARE @RunId int IF NOT EXISTS(SELECT [RunId] FROM [dbo].[Flow_Run] WHERE [RunName] = @RunName) BEGIN INSERT INTO [dbo].[Flow_Run](RunName, FlowId, AddTime, Status) VALUES(@RunName, @FlowId, GETDATE(), 0) SET @RunId = @@IDENTITY IF ISNULL(@RunId,0) <> 0 BEGIN INSERT INTO [dbo].[Flow_Run_Prcs] (RunId, PrcsId, UserId, PrcsFlag, FlowPrcs, OpFlag, TopFlag) VAlUES (@RunId, 1, @UserId, 1, 1, 1, 0) END END ELSE SET @RunId=-1 SELECT @RunId";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@FlowId", SqlDbType.Int);
            commandParameters[0].Value = flowId;
            commandParameters[1] = new SqlParameter("@RunName", SqlDbType.NVarChar, 200);
            commandParameters[1].Value = runName;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != null)
            {
                runId = Convert.ToInt32(obj2);
            }
            if (runId == -1)
            {
                return -1;
            }
            DataTable table = new FlowFormTypeDao().SelectByRunId(conn, runId);
            if (table.Rows.Count > 0)
            {
                string str2 = (table.Rows[0]["FormModel"] != DBNull.Value) ? table.Rows[0]["FormModel"].ToString() : "";
                ArrayList originalValue = new ArrayList();
                FormRegex regex = new FormRegex();
                regex.HtmlSource = str2;
                originalValue = regex.GetOriginalValue();
                FlowRunDataDao dao = new FlowRunDataDao();
                FlowRunDataData data = new FlowRunDataData();
                if (originalValue.Count <= 0)
                {
                    return runId;
                }
                for (int i = 0; i < originalValue.Count; i++)
                {
                    data.RunId = runId;
                    data.ItemId = i + 1;
                    data.ItemData = originalValue[i].ToString();
                    dao.Insert(conn, data);
                }
            }
            return runId;
        }

        public DataTable NoFinishUser(SqlConnection conn, int runId)
        {
            string commandText = "SELECT [Flow_Run_Prcs].[UserId], [Users].[UserName] FROM [dbo].[Flow_Run_Prcs], [dbo].[Users] WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[PrcsFlag] in (1,2) AND [Flow_Run_Prcs].[UserId] = [Users].[UserId]";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable Often(SqlConnection conn, string userId)
        {
            string commandText = "DECLARE @Often TABLE( FlowId int, RunId int ) DECLARE @Often2 TABLE( FlowId int, RunId int ) INSERT INTO @Often SELECT TOP 20 [Flow_Type].[FlowId], [Flow_Run].[RunId] FROM [dbo].[Flow_Type], [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs] WHERE [Flow_Type].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs].[PrcsId] = 1 AND [Flow_Run_Prcs].[UserId] = @UserId ORDER BY [Flow_Run].[RunId] DESC INSERT INTO @Often2 SELECT FlowId = CASE WHEN RunId = (SELECT TOP 1 RunId FROM @Often WHERE FlowId = x.FlowId ORDER BY RunId DESC) THEN FlowId ELSE NULL END,RunId FROM @Often AS x ORDER BY RunId DESC SELECT TOP 8 Often.FlowId, Often.RunId, [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Flow_Run].[RunName], [Flow_Run].[AddTime] FROM @Often2 AS Often, [dbo].[Flow_Type], [dbo].[Flow_Run], [dbo].[Flow_Sort] WHERE Often.FlowId IS NOT NULL AND Often.FlowId = Flow_Type.FlowId AND Flow_Type.FlowSort = Flow_Sort.SortId AND Often.RunId = Flow_Run.RunId ORDER BY Often.RunId DESC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable PassUser(SqlConnection conn, int runId)
        {
            string commandText = "SELECT DISTINCT [Users].[UserId], [Users].[UserName] FROM [dbo].[Flow_Run_Prcs], [dbo].[Users] WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[UserId] = [Users].[UserId]";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Users";
            return set.Tables[0];
        }

        public DataTable Processed(SqlConnection conn, string userId)
        {
            string commandText = "DECLARE @WorkTable TABLE( RunId int, RunName nvarchar(200) DEFAULT '', PrcsId int, FlowPrcs int, PrcsName nvarchar(200) DEFAULT '', FlowId int, FlowName nvarchar(200) DEFAULT '', FlowType int, SortName nvarchar(200) DEFAULT '', OpFlag int, CreateUser nvarchar(20) DEFAULT '', CreateUserName nvarchar(50) DEFAULT '' ) INSERT INTO @WorkTable SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[FlowPrcs], [Flow_Process].[PrcsName], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Flow_Run_Prcs].[OpFlag], [Flow_Run_Prcs2].[UserId] AS [CreateUser], [Users].[UserName] AS [CreateUserName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Run_Prcs] AS [Flow_Run_Prcs2], [dbo].[Flow_Process], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run_Prcs].[PrcsFlag] = 3 AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs2].[RunId] = [Flow_Run].[RunId] AND [Flow_Run_Prcs2].[PrcsId] = 1 AND [Flow_Run_Prcs2].[UserId] = [Users].[UserId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Process].[PrcsId] = [Flow_Run_Prcs].[FlowPrcs] AND [Flow_Type].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 1 ORDER BY [Flow_Run].[RunId] DESC INSERT INTO @WorkTable SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[FlowPrcs], '', [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Flow_Run_Prcs].[OpFlag], [Flow_Run_Prcs2].[UserId] AS [CreateUser], [Users].[UserName] AS [CreateUserName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Run_Prcs] AS [Flow_Run_Prcs2], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run_Prcs].[PrcsFlag] = 3 AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs2].[RunId] = [Flow_Run].[RunId] AND [Flow_Run_Prcs2].[PrcsId] = 1 AND [Flow_Run_Prcs2].[UserId] = [Users].[UserId] AND [Flow_Type].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 2 ORDER BY [Flow_Run].[RunId] DESC SELECT * FROM @WorkTable AS Flow_Run ORDER BY RunId DESC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable Processing(SqlConnection conn, string userId)
        {
            string commandText = "DECLARE @WorkTable TABLE( RunId int, RunName nvarchar(200) DEFAULT '', PrcsId int, FlowPrcs int, PrcsTime DateTime, PrcsName nvarchar(200) DEFAULT '', TimeOut int, FlowId int, FlowName nvarchar(200) DEFAULT '', FlowType int, SortName nvarchar(200) DEFAULT '', OpFlag int, CreateUser nvarchar(20) DEFAULT '', CreateUserName nvarchar(50) DEFAULT '', NowTime DateTime ) INSERT INTO @WorkTable SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[FlowPrcs], [Flow_Run_Prcs].[PrcsTime], [Flow_Process].[PrcsName], [Flow_Process].[TimeOut], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Flow_Run_Prcs].[OpFlag], [Flow_Run_Prcs2].[UserId] AS [CreateUser], [Users].[UserName] AS [CreateUserName], GETDATE() FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Run_Prcs] AS [Flow_Run_Prcs2], [dbo].[Flow_Process], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run_Prcs].[PrcsFlag] = 2 AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs2].[RunId] = [Flow_Run].[RunId] AND [Flow_Run_Prcs2].[PrcsId] = 1 AND [Flow_Run_Prcs2].[UserId] = [Users].[UserId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Process].[PrcsId] = [Flow_Run_Prcs].[FlowPrcs] AND [Flow_Type].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 1 ORDER BY [Flow_Run].[RunId] DESC INSERT INTO @WorkTable SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[FlowPrcs], [Flow_Run_Prcs].[PrcsTime], '', NULL, [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], [Flow_Run_Prcs].[OpFlag], [Flow_Run_Prcs2].[UserId] AS [CreateUser], [Users].[UserName] AS [CreateUserName], GETDATE() FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Run_Prcs] AS [Flow_Run_Prcs2], [dbo].[Flow_Type], [dbo].[Flow_Sort], [dbo].[Users] WHERE [Flow_Run_Prcs].[PrcsFlag] =2 AND [Flow_Run_Prcs].[UserId] = @UserId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs2].[RunId] = [Flow_Run].[RunId] AND [Flow_Run_Prcs2].[PrcsId] = 1 AND [Flow_Run_Prcs2].[UserId] = [Users].[UserId] AND [Flow_Type].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND [Flow_Type].[FlowType] = 2 ORDER BY [Flow_Run].[RunId] DESC SELECT * FROM @WorkTable AS Flow_Run ORDER BY RunId DESC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
            commandParameters[0].Value = userId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable PublicAttachInfo(SqlConnection conn, int runId, ref bool flowDoc, ref string attachmentId)
        {
            string commandText = "SELECT @FlowDoc = [Flow_Type].[FlowDoc], @AttachmentId = [Flow_Run].[AttachmentId] FROM [dbo].[Flow_Type], [dbo].[Flow_Run] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] IF (( ISNULL(@AttachmentId,'')) <> '') AND (@FlowDoc = 1) BEGIN SELECT * FROM [dbo].[Attachment] WHERE [AttachmentId] = @AttachmentId END";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@FlowDoc", SqlDbType.Bit);
            commandParameters[1].Direction = ParameterDirection.Output;
            commandParameters[2] = new SqlParameter("@AttachmentId", SqlDbType.NVarChar, 50);
            commandParameters[2].Direction = ParameterDirection.Output;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            if (commandParameters[1].Value != DBNull.Value)
            {
                flowDoc = Convert.ToBoolean(commandParameters[1].Value);
            }
            if (commandParameters[2].Value != DBNull.Value)
            {
                attachmentId = Convert.ToString(commandParameters[2].Value);
            }
            if (flowDoc && (attachmentId != string.Empty))
            {
                set.Tables[0].TableName = "Attachment";
                return set.Tables[0];
            }
            return new DataTable("Attachment");
        }

        public DataTable QueryRun(SqlConnection conn, string userId, bool isAdministrator)
        {
            string commandText = "IF @IsAdministrator =1 BEGIN SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run].[AddTime], [Flow_Run].[Status], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], @IsAdministrator AS IsAdministrator FROM [dbo].[Flow_Run], [dbo].[Flow_Sort], [dbo].[Flow_Type] WHERE [Flow_Run].[Status] = 0 AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] ORDER BY [Flow_Run].[RunId] DESC END ELSE BEGIN SELECT [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run].[AddTime], [Flow_Run].[Status], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], @IsAdministrator AS IsAdministrator FROM [dbo].[Flow_Run], [dbo].[Flow_Sort], [dbo].[Flow_Type] WHERE [Flow_Run].[Status] = 0 AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND( ([Flow_Run].[RunId] IN ( SELECT DISTINCT [Flow_Run].[RunId] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs] WHERE [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run].[Status] = 0 AND [Flow_Run_Prcs].[UserId] = @UserId ) ) OR ( CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)),'') + ',') > 0 OR CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[QueryUser] AS nvarchar(2000)),'') + ',') > 0 ) ) ORDER BY [Flow_Run].[RunId] DESC END";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[0].Value = userId;
            commandParameters[1] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[1].Value = isAdministrator;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable QueryTop10(SqlConnection conn, string userId, bool isAdministrator)
        {
            string commandText = "IF @IsAdministrator =1 BEGIN SELECT TOP 10 [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run].[AddTime], [Flow_Run].[Status], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], @IsAdministrator AS IsAdministrator FROM [dbo].[Flow_Run], [dbo].[Flow_Sort], [dbo].[Flow_Type] WHERE [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] ORDER BY [Flow_Run].[RunId] DESC END ELSE BEGIN SELECT TOP 10 [Flow_Run].[RunId], [Flow_Run].[RunName], [Flow_Run].[AddTime], [Flow_Run].[Status], [Flow_Type].[FlowId], [Flow_Type].[FlowName], [Flow_Type].[FlowType], [Flow_Sort].[SortName], @IsAdministrator AS IsAdministrator FROM [dbo].[Flow_Run], [dbo].[Flow_Sort], [dbo].[Flow_Type] WHERE [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Type].[FlowSort] = [Flow_Sort].[SortId] AND( ([Flow_Run].[RunId] IN ( SELECT DISTINCT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [UserId] = @UserId ) ) OR ( CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)),'') + ',') > 0 OR CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[QueryUser] AS nvarchar(2000)),'') + ',') > 0 ) ) ORDER BY [Flow_Run].[RunId] DESC END";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[0].Value = userId;
            commandParameters[1] = new SqlParameter("@IsAdministrator", SqlDbType.Bit);
            commandParameters[1].Value = isAdministrator;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run";
            return set.Tables[0];
        }

        public DataTable RunProcessByDept(SqlConnection conn, int runId, int prcsId)
        {
            string commandText = "DECLARE @PrcsUser nvarchar(4000) DECLARE @PrcsDept nvarchar(2000) DECLARE @PrcsPriv nvarchar(2000) SELECT @PrcsUser = CAST([PrcsUser] AS nvarchar(4000)), @PrcsDept = CAST([PrcsDept] AS nvarchar(2000)), @PrcsPriv = CAST([PrcsPriv] AS nvarchar(2000)) FROM [dbo].[Flow_Run], [dbo].[Flow_Process] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Process].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Process].[PrcsId] = @PrcsId SELECT [DeptId], [ParentDept], [DeptName] FROM [dbo].[Department] WHERE ((@PrcsDept = 'ALL_DEPT' OR CHARINDEX(',' + CAST([DeptId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsDept,'') + ',') > 0) OR [DeptId] IN ( SELECT DISTINCT [DeptId] FROM [dbo].[Users] WHERE (CHARINDEX(',' + [UserId] + ',',',' + ISNULL(@PrcsUser,'') + ',') > 0) OR (CHARINDEX(',' + CAST([PrivId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsPriv,'') + ',') > 0) ) ) AND [DeptId] <> -1 ORDER BY [DeptNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Department";
            return set.Tables[0];
        }

        public DataTable RunProcessByPriv(SqlConnection conn, int runId, int prcsId)
        {
            string commandText = "DECLARE @PrcsUser nvarchar(4000) DECLARE @PrcsDept nvarchar(2000) DECLARE @PrcsPriv nvarchar(2000) SELECT @PrcsUser = CAST([PrcsUser] AS nvarchar(4000)), @PrcsDept = CAST([PrcsDept] AS nvarchar(2000)), @PrcsPriv = CAST([PrcsPriv] AS nvarchar(2000)) FROM [dbo].[Flow_Run], [dbo].[Flow_Process] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Process].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Process].[PrcsId] = @PrcsId SELECT [PrivId], [PrivName] FROM [dbo].[User_Priv] WHERE (CHARINDEX(',' + CAST([PrivId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsPriv,'') + ',') > 0) OR [PrivId] IN ( SELECT DISTINCT [PrivId] FROM [dbo].[Users] WHERE ( @PrcsDept = 'ALL_DEPT' OR (CHARINDEX(',' + [UserId] + ',',',' + ISNULL(@PrcsUser,'') + ',') > 0) OR (CHARINDEX(',' + CAST([DeptId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsDept,'') + ',') > 0) ) AND [DeptId] <> -1 ) ORDER BY [PrivNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "User_Priv";
            return set.Tables[0];
        }

        public DataTable RunProcessByUser(SqlConnection conn, int runId, int prcsId)
        {
            string commandText = "DECLARE @PrcsUser nvarchar(4000) DECLARE @PrcsDept nvarchar(2000) DECLARE @PrcsPriv nvarchar(2000) SELECT @PrcsUser = CAST([PrcsUser] AS nvarchar(4000)), @PrcsDept = CAST([PrcsDept] AS nvarchar(2000)), @PrcsPriv = CAST([PrcsPriv] AS nvarchar(2000)) FROM [dbo].[Flow_Run], [dbo].[Flow_Process] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Process].[FlowId] = [Flow_Run].[FlowId] AND [Flow_Process].[PrcsId] = @PrcsId SELECT [Users].[UserId], [Users].[UserName], [Users].[DeptId], [Department].[DeptName], [Users].[PrivId], [User_Priv].[PrivName] FROM [dbo].[Users], [dbo].[Department], [dbo].[User_Priv] WHERE ( @PrcsDept = 'ALL_DEPT' OR (@PrcsDept = 'ALL_DEPT' OR CHARINDEX(',' + [Users].[UserId] + ',',',' + ISNULL(@PrcsUser,'') + ',') > 0) OR (CHARINDEX(',' + CAST([Users].[DeptId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsDept,'') + ',') > 0) OR (CHARINDEX(',' + CAST([Users].[PrivId] AS nvarchar(20)) + ',',',' + ISNULL(@PrcsPriv,'') + ',') > 0) ) AND [Users].[DeptId] <> -1 AND [Users].[DeptId] = [Department].[DeptId] AND [Users].[PrivId] = [User_Priv].[PrivId] ORDER BY [User_Priv].[PrivNo] ASC, [Department].[DeptNo] ASC, [Users].[UserNo] ASC";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Users";
            return set.Tables[0];
        }

        public void RunProcessCallBack(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "IF EXISTS( SELECT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [UserId] = @UserId AND [PrcsFlag] = 3 AND [OpFlag] = 1 ) BEGIN DELETE FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] > @PrcsId UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 2, [PrcsTime] = ISNULL([PrcsTime],GETDATE()), [DeliverTime] = NULL WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [PrcsFlag] = 3 END";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public void RunProcessFixupFinish(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "IF EXISTS( SELECT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [UserId] = @UserId AND [PrcsFlag] = 2 AND [OpFlag] = 1 ) BEGIN UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4, [DeliverTime] = GETDATE() WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [OpFlag] = 1 UPDATE [Flow_Run_Prcs] SET [PrcsFlag] = 4 WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [OpFlag] = 0 UPDATE [Flow_Run] SET [Status] = 2 WHERE [RunId] = @RunId END";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public void RunProcessFixupFinishForManage(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "DECLARE @IsManageUser bit IF EXISTS( SELECT [Flow_Run].[RunId] FROM [dbo].[Flow_Run], [dbo].[Flow_Type] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)),'') + ',') > 0 ) SET @IsManageUser = 1 ELSE SET @IsManageUser = 0 IF @IsManageUser = 1 BEGIN IF @PrcsId > 1 BEGIN UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4 WHERE [RunID] = @RunId AND [PrcsId] = @PrcsId-1 AND [PrcsFlag] <> 4 END UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4, [DeliverTime] = ISNULL([DeliverTime],GETDATE()) WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [OpFlag] = 1 UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4 WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [OpFlag] = 0 UPDATE [Flow_Run] SET [Status] = 2 WHERE [RunId] = @RunId END";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public DataTable RunProcessFixupNextInfo(SqlConnection conn, int runId, int prcsId)
        {
            DataView view;
            int num = 1;
            string str = string.Empty;
            DataTable table = new DataTable("Flow_Process");
            table.Columns.Add("PrcsId", typeof(int));
            table.Columns.Add("PrcsName", typeof(string));
            table.Columns.Add("Plugin", typeof(string));
            table.Columns.Add("PrcsIn", typeof(string));
            table.Columns.Add("PrcsInSet", typeof(string));
            DataTable table2 = new WorkFlowBiz().RunProcessGetInfo(conn, runId, prcsId);
            if (table2.Rows.Count > 0)
            {
                num = Convert.ToInt32(table2.Rows[0]["FlowPrcs"].ToString());
            }
            DataTable table3 = new FlowProcessDao().SelectByRunId(conn, runId);
            DataView view2 = new DataView(table3);
            view2.RowFilter = "PrcsId=" + num.ToString();
            if ((view2.Count > 0) && (view2[0]["PrcsTo"] != DBNull.Value))
            {
                str = view2[0]["PrcsTo"].ToString();
            }
            if (str != string.Empty)
            {
                if (str.EndsWith(","))
                {
                    str = str.Substring(0, str.Length - 1);
                }
                string[] strArray = str.Split(new char[] { ',' });
                if (strArray.Length > 0)
                {
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (strArray[i] == "0")
                        {
                            table.Rows.Add(new object[] { 0, "结束流程", string.Empty });
                        }
                        else
                        {
                            view = new DataView(table3);
                            view.RowFilter = "PrcsId=" + strArray[i];
                            if (view.Count > 0)
                            {
                                table.Rows.Add(new object[] { Convert.ToInt32(view[0]["PrcsId"].ToString()), view[0]["PrcsName"].ToString(), (view[0]["Plugin"] != DBNull.Value) ? view[0]["Plugin"].ToString() : string.Empty, (view[0]["PrcsIn"] != DBNull.Value) ? view[0]["PrcsIn"].ToString() : string.Empty, (view[0]["PrcsInSet"] != DBNull.Value) ? view[0]["PrcsInSet"].ToString() : string.Empty });
                            }
                        }
                    }
                }
                return table;
            }
            view = new DataView(table3);
            view.RowFilter = "PrcsId > " + num.ToString();
            view.Sort = "PrcsId ASC";
            if (view.Count > 0)
            {
                table.Rows.Add(new object[] { Convert.ToInt32(view[0]["PrcsId"].ToString()), view[0]["PrcsName"].ToString(), (view[0]["Plugin"] != DBNull.Value) ? view[0]["Plugin"].ToString() : string.Empty, (view[0]["PrcsIn"] != DBNull.Value) ? view[0]["PrcsIn"].ToString() : string.Empty, (view[0]["PrcsInSet"] != DBNull.Value) ? view[0]["PrcsInSet"].ToString() : string.Empty });
                return table;
            }
            table.Rows.Add(new object[] { 0, "结束流程", string.Empty });
            return table;
        }

        public DataTable RunProcessFixupNoFinishUser(SqlConnection conn, int runId, int prcsId)
        {
            string commandText = "SELECT [Flow_Run_Prcs].[UserId], [Users].[UserName] FROM [dbo].[Flow_Run_Prcs], [dbo].[Users] WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[PrcsId] = @PrcsId AND [Flow_Run_Prcs].[UserId] = [Users].[UserId] AND [Flow_Run_Prcs].[PrcsFlag] <> 4";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Users";
            return set.Tables[0];
        }

        public void RunProcessFixupNoOpFinish(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4, DeliverTime = GETDATE() WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[PrcsId] = @PrcsId AND [Flow_Run_Prcs].[UserId] = @UserId AND [OpFlag] = 0 AND [PrcsFlag] IN(2)";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public DataTable RunProcessFixupPassed(SqlConnection conn, int runId)
        {
            DataTable table = new DataTable("Flow_Run_Prcs");
            table.Columns.Add("PrcsId", typeof(int));
            table.Columns.Add("PrcsName", typeof(string));
            table.Columns.Add("Plugin", typeof(string));
            table.Columns.Add("UserIdStr", typeof(string));
            table.Columns.Add("UserNameStr", typeof(string));
            string commandText = "SELECT [Flow_Run_Prcs].[PrcsId], [Flow_Process].[PrcsName], [Flow_Process].[Plugin], [Flow_Run_Prcs].[UserId], [Flow_Run_Prcs].[PrcsFlag], [Flow_Run_Prcs].[OpFlag], [Users].[UserName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Process], [dbo].[Users] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Run_Prcs].[FlowPrcs] = [Flow_Process].[PrcsId] AND [Flow_Run_Prcs].[UserId] = [Users].[UserId] ORDER BY [Flow_Run_Prcs].[PrcsId] ASC, [Flow_Run_Prcs].[OpFlag] DESC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run_Prcs";
            DataTable table2 = set.Tables[0];
            if (table2.Rows.Count > 0)
            {
                ArrayList list = new ArrayList();
                for (int i = 0; i < table2.Rows.Count; i++)
                {
                    if (!list.Contains(table2.Rows[i]["PrcsId"].ToString()))
                    {
                        list.Add(table2.Rows[i]["PrcsId"].ToString());
                    }
                }
                string str2 = string.Empty;
                string str3 = string.Empty;
                string str4 = string.Empty;
                for (int j = 0; j < list.Count; j++)
                {
                    DataView view = new DataView(table2);
                    view.RowFilter = "PrcsId = " + list[j].ToString();
                    if (view.Count > 0)
                    {
                        str2 = string.Empty;
                        str3 = string.Empty;
                        str4 = string.Empty;
                        for (int k = 0; k < view.Count; k++)
                        {
                            str2 = str2 + view[k]["UserId"].ToString() + ",";
                            switch (Convert.ToInt32(view[k]["PrcsFlag"].ToString()))
                            {
                                case 1:
                                    str3 = str3 + "<font color=red title=未接收办理>" + view[k]["UserName"].ToString() + "(未接收)</font>,";
                                    break;

                                case 2:
                                    str3 = str3 + "<font color=red title=正在办理>" + view[k]["UserName"].ToString() + "(办理中)</font>,";
                                    break;

                                case 3:
                                    str3 = str3 + "<font color=green title=已办理完毕>" + view[k]["UserName"].ToString() + "(已办结)</font>,";
                                    break;

                                case 4:
                                    str3 = str3 + "<font color=green title=已办理完毕>" + view[k]["UserName"].ToString() + "(已办结)</font>,";
                                    break;
                            }
                        }
                        if (str2.EndsWith(","))
                        {
                            str2 = str2.Substring(0, str2.Length - 1);
                        }
                        if (str3.EndsWith(","))
                        {
                            str3 = str3.Substring(0, str3.Length - 1);
                        }
                        if (view[0]["Plugin"] != DBNull.Value)
                        {
                            str4 = view[0]["Plugin"].ToString();
                        }
                        table.Rows.Add(new object[] { Convert.ToInt32(view[0]["PrcsId"].ToString()), view[0]["PrcsName"].ToString(), str4, str2, str3 });
                    }
                }
            }
            return table;
        }

        public DataTable RunProcessFreePassed(SqlConnection conn, int runId)
        {
            string commandText = "SELECT [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[PrcsFlag], [Users].[UserName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Users] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run_Prcs].[UserId] = [Users].[UserId] ORDER BY [Flow_Run_Prcs].[PrcsId] ASC";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run_Prcs";
            return set.Tables[0];
        }

        public void RunProcessFreeStop(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "IF EXISTS( SELECT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [UserId] = @UserId AND [PrcsFlag] IN(2) AND [OpFlag] = 1 ) BEGIN UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4, [DeliverTime] = GETDATE() WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId UPDATE [dbo].[Flow_Run] SET [Status] = 2 WHERE [RunId] = @RunId END";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public void RunProcessFreeStopForManage(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "IF EXISTS( SELECT [Flow_Run].[RunId] FROM [dbo].[Flow_Run], [dbo].[Flow_Type] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)),'') + ',') > 0 ) BEGIN IF @PrcsId > 1 BEGIN UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4 WHERE [RunID] = @RunId AND [PrcsId] = @PrcsId-1 AND [PrcsFlag] <> 4 END UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4, [DeliverTime] = GETDATE() WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId UPDATE [dbo].[Flow_Run] SET [Status] = 2 WHERE [RunId] = @RunId END";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public DataTable RunProcessGetInfo(SqlConnection conn, int runId, int prcsId)
        {
            string commandText = "SELECT [RunId], [PrcsId], [UserId], [PrcsTime], [DeliverTime], [PrcsFlag], [FlowPrcs], [OpFlag], [TopFlag] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run_Prcs";
            return set.Tables[0];
        }

        public bool RunProcessTransact(SqlConnection conn, int runId, int prcsId, string userId)
        {
            string commandText = "IF @PrcsId > 1 BEGIN UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4 WHERE [RunID] = @RunId AND ([PrcsId] < @PrcsId) AND [PrcsFlag] <> 4 END IF EXISTS( SELECT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [UserId] <> @UserId AND [PrcsFlag] = 1 AND [TopFlag] = 1 ) BEGIN DELETE FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [UserId] <> @UserId AND [PrcsFlag] = 1 AND [TopFlag] = 1 END UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 2, [PrcsTime] = getdate() WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [UserId] = @UserId AND [PrcsFlag] = 1";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public void RunProcessTurnNext(SqlConnection conn, int runId, int prcsId, string userId, string toId, int flowPrcs, int opFlag, int topFlag)
        {
            string commandText = "IF EXISTS( SELECT [RunId] FROM [dbo].[Flow_Run_Prcs] WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId-1 AND [UserId] = @UserId AND [PrcsFlag] IN(2,3) AND [OpFlag] = 1 ) BEGIN UPDATE [dbo].[Flow_Run_Prcs] SET [DeliverTime] = GETDATE(), [PrcsFlag] = 3 WHERE [RunID] = @RunId AND [PrcsId] = @PrcsId -1 AND [PrcsFlag] IN(1,2) INSERT INTO [dbo].[Flow_Run_Prcs] (RunId, PrcsId, UserId, PrcsFlag, FlowPrcs, OpFlag, TopFlag) VALUES (@RunId, @PrcsId, @ToId, 1, @FlowPrcs, @OpFlag, @TopFlag) END";
            SqlParameter[] commandParameters = new SqlParameter[7];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            commandParameters[3] = new SqlParameter("@ToId", SqlDbType.NVarChar, 20);
            commandParameters[3].Value = toId;
            commandParameters[4] = new SqlParameter("@FlowPrcs", SqlDbType.Int);
            commandParameters[4].Value = flowPrcs;
            commandParameters[5] = new SqlParameter("@OpFlag", SqlDbType.Int);
            commandParameters[5].Value = opFlag;
            commandParameters[6] = new SqlParameter("@TopFlag", SqlDbType.Int);
            commandParameters[6].Value = topFlag;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public void RunProcessTurnNextForManage(SqlConnection conn, int runId, int prcsId, string userId, string toId, int flowPrcs, int opFlag, int topFlag)
        {
            string commandText = "DECLARE @IsManageUser bit IF EXISTS( SELECT [Flow_Run].[RunId] FROM [dbo].[Flow_Run], [dbo].[Flow_Type] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND CHARINDEX(',' + @UserId + ',',',' + ISNULL(CAST([Flow_Type].[ManageUser] AS nvarchar(2000)),'') + ',') > 0 ) SET @IsManageUser = 1 ELSE SET @IsManageUser = 0 IF @IsManageUser = 1 BEGIN IF @PrcsId >2 BEGIN UPDATE [dbo].[Flow_Run_Prcs] SET [PrcsFlag] = 4 WHERE [RunID] = @RunId AND [PrcsId] = @PrcsId-2 AND [PrcsFlag] <> 4 END UPDATE [dbo].[Flow_Run_Prcs] SET [DeliverTime] = GETDATE(), [PrcsFlag] = 3 WHERE [RunID] = @RunId AND [PrcsId] = @PrcsId -1 INSERT INTO [dbo].[Flow_Run_Prcs] (RunId, PrcsId, UserId, PrcsFlag, FlowPrcs, OpFlag, TopFlag) VALUES (@RunId, @PrcsId, @ToId, 1, @FlowPrcs, @OpFlag, @TopFlag) END";
            SqlParameter[] commandParameters = new SqlParameter[7];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            commandParameters[2] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[2].Value = userId;
            commandParameters[3] = new SqlParameter("@ToId", SqlDbType.NVarChar, 20);
            commandParameters[3].Value = toId;
            commandParameters[4] = new SqlParameter("@FlowPrcs", SqlDbType.Int);
            commandParameters[4].Value = flowPrcs;
            commandParameters[5] = new SqlParameter("@OpFlag", SqlDbType.Int);
            commandParameters[5].Value = opFlag;
            commandParameters[6] = new SqlParameter("@TopFlag", SqlDbType.Int);
            commandParameters[6].Value = topFlag;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public void RunProcessUserChange(SqlConnection conn, string userId, int runId, int prcsId, string toId)
        {
            string commandText = "UPDATE [dbo].[Flow_Run_Prcs] SET [UserId] = @ToId WHERE [RunId] = @RunId AND [PrcsId] = @PrcsId AND [UserId] = @UserId AND [PrcsFlag] IN(1,2)";
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[0].Value = userId;
            commandParameters[1] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[1].Value = runId;
            commandParameters[2] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[2].Value = prcsId;
            commandParameters[3] = new SqlParameter("@ToId", SqlDbType.NVarChar, 20);
            commandParameters[3].Value = toId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
        }

        public DataTable View1(SqlConnection conn, int runId)
        {
            string commandText = "DECLARE @FlowType int DECLARE @ProcessTable table(FlowType int, PrcsId int, FlowPrcs int, PrcsName nvarchar(200) DEFAULT '') SELECT @FlowType = [Flow_Type].[FlowType] FROM [dbo].[Flow_Run], [dbo].[Flow_Type] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] IF @FlowType = 1 INSERT INTO @ProcessTable SELECT DISTINCT 1, [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[FlowPrcs], [Flow_Process].[PrcsName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Process] WHERE [Flow_Run].[RunId] = @RunId AND [Flow_Run].[RunId] = [Flow_Run_Prcs].[RunId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Run_Prcs].[FlowPrcs] = [Flow_Process].[PrcsId] ORDER BY [Flow_Run_Prcs].[PrcsId] ASC ELSE INSERT INTO @ProcessTable SELECT 2, [PrcsId], 0, '' FROM [dbo].[Flow_Run_Prcs] WHERE [RunId]= @RunId ORDER BY [PrcsId] ASC SELECT * FROM @ProcessTable";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@RunId", SqlDbType.Int) };
            commandParameters[0].Value = runId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run_Prcs";
            return set.Tables[0];
        }

        public DataTable View2(SqlConnection conn, int runId, int prcsId)
        {
            string commandText = "DECLARE @ProcessTable TABLE( RunId int, PrcsId int, PrcsTime DateTime, DeliverTime DateTime, TimeOut int, NowTime DateTime, PrcsFlag int, OpFlag int, UserName nvarchar(20) DEFAULT '', DeptName nvarchar(50) DEFAULT '' ) INSERT INTO @ProcessTable SELECT [Flow_Run_Prcs].[RunId], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[PrcsTime], [Flow_Run_Prcs].[DeliverTime], [Flow_Process].[TimeOut], GETDATE() NowTime, [Flow_Run_Prcs].[PrcsFlag], [Flow_Run_Prcs].[OpFlag], [Users].[UserName], [Department].[DeptName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Process], [dbo].[Flow_Type], [dbo].[Users], [dbo].[Department] WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[PrcsId] = @PrcsId AND [Flow_Run_Prcs].[RunId] = [Flow_Run].[RunId] AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Run].[FlowId] = [Flow_Process].[FlowId] AND [Flow_Run_Prcs].[FlowPrcs] = [Flow_Process].[PrcsId] AND [Flow_Type].[FlowType] = 1 AND [Flow_Run_Prcs].[UserId] = [Users].[UserId] AND [Users].[DeptId] = [Department].[DeptId] ORDER BY [Flow_Run_Prcs].[PrcsId] ASC, [Flow_Run_Prcs].[OpFlag] DESC INSERT INTO @ProcessTable SELECT [Flow_Run_Prcs].[RunId], [Flow_Run_Prcs].[PrcsId], [Flow_Run_Prcs].[PrcsTime], [Flow_Run_Prcs].[DeliverTime], NULL, GETDATE() NowTime, [Flow_Run_Prcs].[PrcsFlag], [Flow_Run_Prcs].[OpFlag], [Users].[UserName], [Department].[DeptName] FROM [dbo].[Flow_Run], [dbo].[Flow_Run_Prcs], [dbo].[Flow_Type], [dbo].[Users], [dbo].[Department] WHERE [Flow_Run_Prcs].[RunId] = @RunId AND [Flow_Run_Prcs].[PrcsId] = @PrcsId AND [Flow_Run_Prcs].[RunId] = [Flow_Run].[RunId] AND [Flow_Run].[FlowId] = [Flow_Type].[FlowId] AND [Flow_Type].[FlowType] = 2 AND [Flow_Run_Prcs].[UserId] = [Users].[UserId] AND [Users].[DeptId] = [Department].[DeptId] ORDER BY [Flow_Run_Prcs].[PrcsId] ASC, [Flow_Run_Prcs].[OpFlag] DESC SELECT * FROM @ProcessTable";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@RunId", SqlDbType.Int);
            commandParameters[0].Value = runId;
            commandParameters[1] = new SqlParameter("@PrcsId", SqlDbType.Int);
            commandParameters[1].Value = prcsId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Flow_Run_Prcs";
            return set.Tables[0];
        }
    }
}

